package com.example.springboot.controller;

import com.example.springboot.dto.MerchantRentalDTO;
import com.example.springboot.entity.ClothingRental;
import com.example.springboot.service.ClothingRentalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/rentals")
public class RentalController {

    @Autowired
    private ClothingRentalService clothingRentalService;

    // 商家获取租赁数据
    @GetMapping("/merchant/{merchantId}")
    public MerchantRentalDTO getMerchantRentals(@PathVariable Integer merchantId) {
        return clothingRentalService.getMerchantRentals(merchantId);
    }

    // 商家更新状态
    @PostMapping("/updateStatus")
    public Map<String, Object> updateRentalStatus(@RequestBody Map<String, Object> params) {
        Integer rentalId = (Integer) params.get("rentalId");
        Integer status = (Integer) params.get("status");

        boolean success = clothingRentalService.updateRentalStatus(rentalId, status);
        Map<String, Object> response = new HashMap<>();
        response.put("success", success);
        response.put("message", success ? "更新成功" : "更新失败");
        return response;
    }

    // 商家新增衣物
    @PostMapping("/add")
    public Map<String, Object> addNewRental(
            @RequestParam Integer merchantId,
            @RequestParam(required = false) String setName,
            @RequestParam(required = false) String topName,
            @RequestParam(required = false) String pantsName,
            @RequestParam(required = false) String skirtName,
            @RequestParam(required = false) String shoesName,
            @RequestParam(required = false) String accessoryName,
            @RequestParam Double pricePerHour,
            @RequestParam MultipartFile imageFile,
            @RequestParam Integer quantity,
            @RequestParam String size) {
        Map<String, Object> response = new HashMap<>();
        try {
            boolean success = clothingRentalService.addNewRental(merchantId, setName, topName, pantsName, skirtName,
                    shoesName, accessoryName, pricePerHour, imageFile,
                    quantity, size);
            response.put("success", success);
            response.put("message", success ? "商品已添加，待审核" : "商品添加失败");
        } catch (Exception e) {
            response.put("success", false);
            response.put("message", "错误：" + e.getMessage());
        }
        return response;
    }

    // 商家更新库存
    @PutMapping("/updateQuantity/{rentalId}")
    public Map<String, Object> updateRentalQuantity(
            @PathVariable Integer rentalId,
            @RequestBody Map<String, Object> params) {
        Integer quantity = (Integer) params.get("quantity");
        boolean success = clothingRentalService.updateRentalQuantity(rentalId, quantity);
        Map<String, Object> response = new HashMap<>();
        response.put("success", success);
        response.put("message", success ? "数量更新成功" : "数量更新失败");
        return response;
    }

    // 管理员获取衣物列表
    @GetMapping("/admin")
    public Map<String, Object> getAdminRentals(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size,
            @RequestParam(required = false) Integer status,
            HttpServletRequest request) {
        String role = request.getHeader("Role");
        String username = request.getHeader("Username");
        return clothingRentalService.getAdminRentals(page, size, role, username, status);
    }

    // 管理员确认上架状态
    @PostMapping("/admin/updateStatus")
    public Map<String, Object> adminUpdateStatus(
            @RequestBody Map<String, Object> params,
            HttpServletRequest request) {
        Integer rentalId = (Integer) params.get("rentalId");
        Integer status = (Integer) params.get("status");
        String role = request.getHeader("Role");
        String username = request.getHeader("Username");

        boolean success = clothingRentalService.adminUpdateStatus(rentalId, status, role, username);
        Map<String, Object> response = new HashMap<>();
        response.put("success", success);
        response.put("message", success ? "状态更新成功" : "状态更新失败");
        return response;
    }

    // 管理员更新衣物信息
    @PutMapping("/admin/update")
    public Map<String, Object> adminUpdateRental(
            @RequestBody ClothingRental rental,
            HttpServletRequest request) {
        String role = request.getHeader("Role");
        String username = request.getHeader("Username");

        boolean success = clothingRentalService.adminUpdateRental(rental, role, username);
        Map<String, Object> response = new HashMap<>();
        response.put("success", success);
        response.put("message", success ? "衣物更新成功" : "衣物更新失败");
        return response;
    }

    // 管理员删除衣物
    @DeleteMapping("/admin/{rentalId}")
    public Map<String, Object> adminDeleteRental(
            @PathVariable Integer rentalId,
            HttpServletRequest request) {
        String role = request.getHeader("Role");
        String username = request.getHeader("Username");

        boolean success = clothingRentalService.adminDeleteRental(rentalId, role, username);
        Map<String, Object> response = new HashMap<>();
        response.put("success", success);
        response.put("message", success ? "衣物删除成功" : "衣物删除失败");
        return response;
    }
}